package com.lwm.mybatis.annotation.dao;


import com.lwm.mybatis.annotation.pojo.Account;
import com.lwm.mybatis.annotation.pojo.AccountUser;
import com.lwm.mybatis.annotation.pojo.User;
import com.lwm.mybatis.annotation.pojo.UserAccount;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @author liweimin
 * @date 2021/6/17 9:22
 * @since
 */
@CacheNamespace(blocking = true)
public interface UserDAO {

    /**
     * find user
     *
     * @return
     */
    @Select("select * from user ")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "address", column = "address"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "username", column = "username")
    })
    List<User> findAllUser();

    /**
     * findAll
     *
     * @return
     */

    @Select("select * from user ")
    @ResultMap("userAccountMap")
    List<UserAccount> findAll();

    /**
     * findAll
     *
     * @return
     */
    @Select("select * from user ")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "address", column = "address"),
            @Result(property = "accountList", column = "id", many = @Many(select = "com.lwm.mybatis.annotation.dao.UserDAO.findAccountByUserId", fetchType = FetchType.LAZY))
    })
    List<UserAccount> findAllUserAccount();

    /**
     * select
     *
     * @param uid
     * @return
     */
    @Select("select * from account where uid = #{uid}")
    List<Account> findAccountByUserId(Integer uid);

    /**
     * select
     *
     * @param id
     * @return
     */

    @Select("select * from user where id = #{id}")
    User selectUserById(Integer id);

    /**
     * update
     *
     * @param user
     * @return
     */
    @Update("update user set username = #{username}, sex = #{sex}, birthday = #{birthday} , address = #{address} where id = #{id}")
    Integer updateUser(User user);


    /**
     * select
     *
     * @param id
     * @return
     */
    @Select("select * from account where id = #{id}")
    @Results({@Result(id = true, property = "id", column = "id"),
            @Result(property = "uid", column = "uid"),
            @Result(property = "money", column = "money"),
            @Result(property = "user", column = "uid", one = @One(select = "com.lwm.mybatis.annotation.dao.UserDAO.selectUserById", fetchType = FetchType.EAGER))})
    AccountUser selectAccountUserById(Integer id);
}
